/*---------------------------------------------------------
    ev_classes.lua - Class Setup
	by Lead4u
---------------------------------------------------------*/

evorp.Class = {};
evorp.Class.Classes = {};
local noremove = { "weapon_physcannon", "weapon_physgun", "gmod_camera", "gmod_tool" }

/*---------------------------------------------------------
	Submit Class to Table | Args: CLASS- CLASS table
---------------------------------------------------------*/

	function evorp.Class:NewClass( CLASS )
		evorp.Class.Classes[string.lower(CLASS.Name)] = {};
		
		for k,v in pairs( CLASS ) do
			evorp.Class.Classes[string.lower(CLASS.Name)][k] = v;
		end

	end
	
/*---------------------------------------------------------
	Find Class | Args: cname- class name | Returns: class
---------------------------------------------------------*/
	
	function evorp.Class:Find( cname )
		for k,v in pairs( evorp.Class.Classes ) do
			if ( string.lower(k) == cname ) then
				return evorp.Class.Classes[k]
			end
		end
	end
	
/*---------------------------------------------------------
	Find Player Class
		Args: cname- class | Returns: class
---------------------------------------------------------*/
	
	function evorp.Class:ClassFindByPlayer( player )
	
		return evorp.players[player:UniqueID()].class	
	end
	
/*---------------------------------------------------------
	Change Class | Args: cname- class name
---------------------------------------------------------*/
	
	function evorp.Class:Join( player, cname )
		
		local class = evorp.Class:Find( string.lower( string.Trim(cname) ) )
		player:SetModel( class.Model )
		evorp.players[player:UniqueID()].class = {};
		evorp.players[player:UniqueID()].class = string.lower( cname );
		gamemode.Call("PlayerLoadout", player)
	
	end
	
/*---------------------------------------------------------
    Set Class Equipment
---------------------------------------------------------*/	

	function evorp.LoadOut( player )
		local class = evorp.Class:Find( evorp.Class:ClassFindByPlayer( player ) )
		
		player:Give("gmod_camera")
		player:Give("weapon_physgun")
		player:Give("gmod_tool")
		player:Give("weapon_physcannon")
		player:SelectWeapon("weapon_physcannon")
		
		for k,v in pairs( player:GetWeapons() ) do
			if ( !table.HasValue( noremove, v:GetClass() ) ) then
				player:StripWeapon( v:GetClass() )
			end
		end
		
		for k,v in pairs( class.Equipment ) do
			player:Give( v )
		end
		
		player:SelectWeapon( "weapon_physgun" )
		return true;
	end
	hook.Add( "PlayerLoadout", "ev_playerloadout", evorp.LoadOut, evorp)

/*---------------------------------------------------------
	Demote to Citizen Class | Args: cname- class name
---------------------------------------------------------*/
	
	function evorp.Class:Remove( player, cname )
		evorp.Class:Join( player, "Citizen");
	end
	
/*---------------------------------------------------------
	Temp Chat Command to Join Class
---------------------------------------------------------*/

	local function playerSaid( player, saywhat )
		if string.find(string.lower(saywhat), "/class") == 1 then
			evorp.Class:Join( player, string.lower(string.sub(saywhat, 8, string.len(saywhat))))
		end
	end
	 
	hook.Add ( "PlayerSay", "ev_playerSaid", playerSaid )